package com.meizu.llp.cameratest;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Environment;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by llp on 16-11-16.
 */

public class CameraUtil {
    //Camera帮助类，实现查找cameraId等功能

    /**
     * 检测当前设备的Camera是否可用
     * @param context
     * @return
     */
    public static boolean checkCameraHardware(Context context){
        //通过系统是否拥有此特征的方法判断
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)
                ||context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 开启Camera服务
     * @return
     */
    public static Camera getCameraInstance(int cameraId){
        Camera camera = null;
        try {
            camera = Camera.open(cameraId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return camera;
    }

    /**
     * 根据type--前后摄像头找到相应的cameraId
     * @param type
     * @return
     */
    public static int findFacingCameraId(Consts.CameraType type){
        int cameraId = -1;
        int frontCameraId=-1;
        int backCameraId=-1;
        int numberOfCameras = Camera.getNumberOfCameras();//返回此设备中可用的相机的数量
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(i, info);//根据camera的编号找到其CameraInfo
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                frontCameraId = i;
            }
            if (info.facing==Camera.CameraInfo.CAMERA_FACING_BACK) {
                backCameraId = i;
            }
        }
        cameraId = (type.equals(Consts.CameraType.CAMERA_BACK)?backCameraId:frontCameraId);
        return cameraId;
    }


    /**
     * 获取存储文件的位置
     * @return
     */
    public static File getOutputImageFile(){
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures");
        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdir()){
                System.out.println("创建文件夹失败");
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        System.out.println(mediaFile.getAbsolutePath());
        return mediaFile;
    }

    /**
     * 设置使用后置摄像头时的属性
     * @param camera
     */
    public static void setBackCameraParameters(Camera camera){
        //设置保存图片的属性为顺时针旋转90度--为了让保存的图片和拍照的orientation一致
        Camera.Parameters parameters = camera.getParameters();
        parameters.setRotation(90);
        camera.setParameters(parameters);
        //设置camera预览显示位置为顺时针旋转90度--为了在拍照的时候使得其和手机orientation一致
        camera.setDisplayOrientation(90);
    }

    /**
     * 设置使用前置摄像头时的属性
     * @param camera
     */
    public static void setFrontCameraParameters(Camera camera){
        //设置保存图片的属性为顺时针旋转90度--为了让保存的图片和拍照的orientation一致
        Camera.Parameters parameters = camera.getParameters();
        parameters.setRotation(270);
        camera.setParameters(parameters);
        //设置camera预览显示位置为顺时针旋转90度--为了在拍照的时候使得其和手机orientation一致
        camera.setDisplayOrientation(90);
    }

    /**
     * 设置后置摄像头的cameraType
     * @param camera
     * @param cameraType
     * @param flashMode
     */
    public static void setFlashModeInBackCamera(Camera camera, Consts.CameraType cameraType,Consts.FlashMode flashMode){
        if (cameraType== Consts.CameraType.CAMERA_FRONT){
            return;
        }
        Camera.Parameters parameters = camera.getParameters();
        switch (flashMode){
            case  FLASH_ON:
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                break;
            case FLASH_OFF:
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                break;
            case FLASH_AUTO:
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
                break;
        }
        camera.setParameters(parameters);
    }
}
